import { cloneDeep, findIndex, uniqueId } from 'lodash'
import { getCurrentInstance } from 'vue'
import { Messages, confirm } from '@Config/config'
import { message } from 'ant-design-vue'

import { useRoute } from 'vue-router'
import { useWin } from '@Hooks/win'
import CateForm from '@Page/mark/components/cateForm'
import markForm from '@Page/mark/components/markForm'
import { tagAdd } from '@Config/server/api'

export const instanceFn = () => {
  let { proxy } = getCurrentInstance()

  let [cateFormWin] = useWin({
    component: (
      <CateForm
        onResult={async ({ info, iCallBack }) => {
          let _r = await tagAdd({ ...info })
          iCallBack(_r)
          if (_r.code == 0) {
            Messages.add(message.success('操作成功'))
            if (info.id == 0) {
              //新建模式

              proxy.tableData.data.source.push({ ..._r.data, child: [] })
            } else {
              //编辑模式
              let _index = findIndex(proxy.tableData.data.source, {
                id: info.id,
              })
              if (_index >= 0) {
                let newItem = {
                  ..._r.data,
                  child: cloneDeep(proxy.tableData.data.source[_index].child),
                }
                proxy.tableData.data.source.splice(_index, 1, newItem)
              }
            }
          } else {
            Messages.add(message.error(`操作失败[${_r.msg}]`))
          }
        }}
      />
    ),
  })

  let [markFormWin] = useWin({
    component: (
      <markForm
        onResult={async ({ info, iCallBack }) => {
          let _r = await tagAdd({ ...info })

          if (_r.code == 0) {
            Messages.add(message.success('操作成功'))
            let _index = findIndex(proxy.tableData.data.source, {
              id: info.pid,
            })

            if (info.id == 0) {
              //新建标签
              if (_index >= 0) {
                proxy.tableData.data.source[_index].child.push({ ..._r.data })
              } else {
                console.error('----')
              }
            } else {
              //编辑标签
              let _index2 = findIndex(
                proxy.tableData.data.source[_index].child,
                { id: info.id }
              )
              let newItem = {
                ..._r.data,
              }
              if (_index2 >= 0) {
                //没有换分类
                proxy.tableData.data.source[_index].child.splice(
                  _index2,
                  1,
                  newItem
                )
              } else {
                //删除旧的

                let _rIndex = findIndex(proxy.tableData.data.source, {
                  id: info.originId,
                })
                let _rIndex2 = findIndex(
                  proxy.tableData.data.source[_rIndex].child,
                  { id: info.id }
                )
                proxy.tableData.data.source[_rIndex].child.splice(_rIndex2, 1)

                //已换分类
                proxy.tableData.data.source[_index].child.push(newItem)
              }
            }
          } else {
            Messages.add(message.error(`操作失败[${_r.msg}]`))
          }
          iCallBack(_r)
        }}
      />
    ),
  })

  return { cateFormWin, markFormWin }
}
